package com.ecar.api.async;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.annotation.Autowired;

import com.ecar.api.service.common.NotifyService;

public class AsyncTheadPool {

	private static final int THREAD_POOL_SIZE = 32;
	private static final int MAX_HANDLER = 5;

	//public static BlockingQueue<List<Refund>> refundQueue;       // 待退款队列
	//public static BlockingQueue<Notify> notifyQueue;		// 通知队列
	public static BlockingQueue<List<String>> smsQueue;		// 短信队列
	//public static BlockingQueue<WsPayload> publishQueue;		// 消息发布队列
	//public static BlockingQueue<Event> eventQueue;

	private Thread single;

	@Autowired
	private NotifyService notifyService;

//	@Autowired
//	private PayService payService;
//
//	@Autowired
//	private JPushService jPushService;
//
//	@Autowired
//	private TaskService taskService;
//
//	@Autowired
//	private RtTaskService rtTaskService;
//
	private static ExecutorService executorService;

	public AsyncTheadPool() { }

	@PostConstruct
	public void executor() {
		//executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
		//refundQueue = new LinkedBlockingQueue<>();
		//notifyQueue = new LinkedBlockingQueue<>();
		smsQueue = new LinkedBlockingQueue<>();
		//publishQueue = new LinkedBlockingQueue<>();
		//eventQueue = new LinkedBlockingQueue<>();

		for (int i = 0; i < MAX_HANDLER; i++) {
			//RefundExecutor refundExecutor = new RefundExecutor(refundQueue, payService);
			//NotifyExecutor notifyExecutor = new NotifyExecutor(notifyQueue, jPushService);
			SMSExecutor smsExecutor = new SMSExecutor(smsQueue, notifyService);
			//Publisher publisher = new Publisher(publishQueue);
			//EventExecutor eventExecutor = new EventExecutor(eventQueue, taskService, rtTaskService);
			//executorService.execute(refundExecutor);
			//executorService.execute(notifyExecutor);
			executorService.execute(smsExecutor);
			//executorService.execute(publisher);
			//executorService.execute(eventExecutor);
		}
		single = new Thread(new Subscriber());
		single.start();
	}

	@PreDestroy
	public void destroy() {
		if (executorService != null) executorService.shutdownNow();
		if (single != null) single.interrupt();
	}
}
